package cn.yanhu.kuwanapp.earn.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.woochen.comlib.BaseActivity
import cn.woochen.comlib.adapter.OnItemClickListener
import cn.woochen.comlib.util.ConfigPref
import cn.woochen.comlib.util.OnSingleClickListener
import cn.woochen.comlib.util.UserPref
import cn.woochen.comlib.util.ext.dp
import cn.woochen.comlib.util.ext.setOnSingleClickListener
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.bean.response.RespGameListBean
import cn.yanhu.kuwanapp.databinding.ActivityGameSearchBinding
import cn.yanhu.kuwanapp.earn.GameViewModel
import cn.yanhu.kuwanapp.earn.adapter.HistoryTagAdapter
import cn.yanhu.kuwanapp.earn.adapter.SearchGameListAdapter
import cn.yanhu.kuwanapp.home.MainActivity
import cn.yanhu.kuwanapp.home.MainViewModel
import cn.yanhu.kuwanapp.utils.NavigatorHelper
import cn.yanhu.kuwanapp.utils.constans.Constants
import cn.yanhu.kuwanapp.utils.ext.parseState
import cn.yanhu.kuwanapp.utils.ext.showToast
import cn.yanhu.kuwanapp.widgets.recycler.RecycleViewDivider

/**
 * 类描述：游戏搜索
 * @author：woochen
 * create at：2021/7/30 5:52 AM
 */
class GameSearchActivity : BaseActivity<ActivityGameSearchBinding, GameViewModel>(),
    OnSingleClickListener {
    private var mKeyWords by ConfigPref(Constants.SEARCH_KEY_WORD, "")
    private val mHistoryTagDatas by lazy {
        mutableListOf<String>()
    }

    private val mHistoryTagAdapter by lazy {
        HistoryTagAdapter(mHistoryTagDatas)
    }

    private val mSearchGameListDatas by lazy {
        mutableListOf<RespGameListBean>()
    }
    private val mSearchGameListAdapter by lazy {
        SearchGameListAdapter(mSearchGameListDatas)
    }

    override fun setContentView(): Int = R.layout.activity_game_search

    override fun initData() {
        setStatusBarStyle(false)
        initWidgets()
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        //游戏搜索
        mViewModel.gameSearchInfo.observe(this, Observer { it ->
            parseState(it,{
                mBinding.llSearchTag.isVisible = false
                mSearchGameListDatas.clear()
                mSearchGameListDatas.addAll(it)
                mSearchGameListAdapter.notifyDataSetChanged()
            })
        })
    }

    override fun initListener() {
        mBinding.tvSearch.setOnSingleClickListener(this)
        mBinding.tvClearHistory.setOnSingleClickListener(this)
    }

    private fun initWidgets() {
        mBinding.rvTag.run {
            val split = mKeyWords.split("$$")
            if (split.isNullOrEmpty()){
                mBinding.llSearchTag.isVisible = false
            }else{
                mHistoryTagDatas.clear()
                split.forEach {
                  if (it.isNotBlank())  mHistoryTagDatas.add(it)
                }
                mBinding.llSearchTag.isVisible = true
                adapter = mHistoryTagAdapter
                addItemDecoration(RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL, 10f.dp.toInt(), mContext.resources.getColor(R.color.colorWhite)))
                addItemDecoration(RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 10f.dp.toInt(), mContext.resources.getColor(R.color.colorWhite)))
                mHistoryTagAdapter.mItemClickListener = OnItemClickListener {
                    mBinding.llSearchTag.isVisible = false
                    val keyWord = mHistoryTagDatas[it]
                    mBinding.etKeyword.setText(keyWord)
                    mViewModel.gameSearch(keyWord)
                }
            }
        }
        mBinding.rvGameList.run {
            adapter = mSearchGameListAdapter
            mSearchGameListAdapter.mItemClickListener = OnItemClickListener {
                val gameBean = mSearchGameListDatas[it]
                NavigatorHelper.toCpl(mContext,gameBean.id)
//                NavigatorHelper.toCpl(mContext,gameBean.detailUrl)
            }
        }
    }

    companion object {

        fun start(context: Context) {
            val intent = Intent(context, GameSearchActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onSingleClick(v: View?) {
        when (v) {
            mBinding.tvSearch -> { //搜索
                val etKeyWord = mBinding.etKeyword.text.trim().toString()
                if (etKeyWord.isBlank()) {
                    showToast("请输入游戏名称!")
                    return
                }
                if (!mHistoryTagDatas.contains(etKeyWord)){
                    if (mHistoryTagDatas.size >9) mHistoryTagDatas.removeAt(mHistoryTagDatas.size -1)
                    mHistoryTagDatas.add(0,etKeyWord)
                    var stringBuffer = StringBuffer()
                    mHistoryTagDatas.forEach {
                        stringBuffer.append("${it}$$")
                    }
                    mKeyWords = stringBuffer.toString()
                }
                mViewModel.gameSearch(etKeyWord)
            }
            mBinding.tvClearHistory -> {//清除搜索历史
                mKeyWords = ""
                mHistoryTagDatas.clear()
                mHistoryTagAdapter.notifyDataSetChanged()
            }
        }
    }

}